﻿@inject INavInvReportClassService NavInvReportClassService


<div align="center">
    <label>Inventory by Month</label>
</div>
<Chart @ref="_barChart" ChartType="ChartType.Bar" OnInitAsync="@OnInit" Height=100% />
@code {
    private Chart _barChart = default!;
    private int _year;
    private List<ChartDoubleDataDto> rawMaterialDataDtos = new();
    private List<ChartDoubleDataDto> semiProductsDataDtos = new();
    private List<ChartDoubleDataDto> finishGoodsTradeDataDtos = new();
    //todo:双轴，展示MTPT，目前不能在bar上叠加曲线图
    // private List<ChartIntDataDto> mtptDataDtos = new();

    private async Task<ChartDataSource> OnInit()
    {
        _year = DateTime.Today.Year;

        var response = await NavInvReportClassService.GetAllAsync();
        if (response.Status)
        {
            var thisYearDtos = response.Result.Where(x => x.YearMonth.Year == _year);
            rawMaterialDataDtos = thisYearDtos.Select(x => new ChartDoubleDataDto
                {
                    Label = x.YearMonth.ToString("yyyy-MM"),
                    Value = x.RawMtlValue
                }).ToList();
            semiProductsDataDtos = thisYearDtos.Select(x => new ChartDoubleDataDto
                {
                    Label = x.YearMonth.ToString("yyyy-MM"),
                    Value = x.SemiProdValue
                }).ToList();
            finishGoodsTradeDataDtos = thisYearDtos.Select(x => new ChartDoubleDataDto
                {
                    Label = x.YearMonth.ToString("yyyy-MM"),
                    Value = x.FgTradeValue
                }).ToList();
            // mtptDataDtos = thisYearDtos.Select(x => new ChartIntDataDto
            // {
            //     Label = x.YearMonth.ToString("yyyy-MM"),
            //     Value = x.MTPT
            // }).ToList();
        }
        var ds = new ChartDataSource();
        
        ds.Options.X.Title = "Month";
        ds.Options.Y.Title = "Inventory";
        ds.Options.X.Stacked = true;
        ds.Options.Y.Stacked = true;
        
        // ds.Options.Y2.Title = "MTPT(Days)";
        // ds.Options.Y2.PositionLeft = false;

        ds.Labels = rawMaterialDataDtos.Select(x => x.Label);
        ds.Data.Add(new ChartDataset
            {
                Label = "RawMaterial",
                Data = rawMaterialDataDtos.Select(x => x.Value).Cast<object>(),
            });
        ds.Data.Add(new ChartDataset
            {
                Label = "Semi-Products",
                Data = semiProductsDataDtos.Select(x => x.Value).Cast<object>(),
            });
        ds.Data.Add(new ChartDataset
            {
                Label = "FinishedGoods/Trade",
                Data = finishGoodsTradeDataDtos.Select(x => x.Value).Cast<object>(),
            });
        // ds.Data.Add(new ChartDataset
        // {
        //     Label = "MTPT",
        //     Data = mtptDataDtos.Select(x => x.Value).Cast<object>(),
        //     IsAxisY2 = true,
        //     Fill = false,
        //     PointStyle = ChartPointStyle.Star
        // });

        return ds;
    }

}
